package cn.lzy.goodshomepage.service.impl;

import cn.cc.common.contants.RedisKeyConstant;
import cn.cc.common.redis.RedisCommon;
import cn.cc.common.result.utils.RestResult;
import cn.cc.goods.service.GoodsTypeService;
import cn.cc.goods.service.domain.GetGoodsSpuDO;
import cn.cc.goods.service.domain.GetGoodsTypeDO;
import cn.lzy.goodshomepage.service.GoodsHomepageService;
import cn.lzy.goodshomepage.service.domain.GoodsHomepage;
import cn.lzy.goodshomepage.service.domain.GoodsHomepageAndSpu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class GoodsHomepageServiceImpl implements GoodsHomepageService {
    @Autowired
    private GoodsTypeService typeService;
    @Autowired
    private RedisCommon redisCommon;
    @Override
    public RestResult<List<GoodsHomepage>> getGoodsHomepage() {
        //取出类型
        Map<String,GetGoodsTypeDO> typeMap = redisCommon.getHashAll(RedisKeyConstant.SHOPPING_CAR_GOODS_CC_GOODS_TYPE, GetGoodsTypeDO.class);
        List<GoodsHomepage> list2=new ArrayList<>();
        //取出type中所有数据
        Iterator<GetGoodsTypeDO> iterator = typeMap.values().iterator();
        while (iterator.hasNext()){
            GetGoodsTypeDO next = iterator.next();
            GoodsHomepage homepage = new GoodsHomepage();
            homepage.setGoods_type_name(next.getGoodsName());
            list2.add(homepage);
        }
        return new RestResult<>(list2);
    }

    @Override
    public RestResult<List<GoodsHomepageAndSpu>> getGoodsHomepageAndSpu(String typeName) {
        //取spu数据
        Map<String,GetGoodsSpuDO> spuMap = redisCommon.getHashAll(RedisKeyConstant.SHOPPING_CAR_GOODS_CC_GOODS_SPU, GetGoodsSpuDO.class);
        //取出类型
        //Map<String,GetGoodsTypeDO> typeMap = redisCommon.getHashAll(RedisKeyConstant.SHOPPING_CAR_GOODS_CC_GOODS_TYPE, GetGoodsTypeDO.class);
        List<GoodsHomepage> list2=new ArrayList<>();
        //取出spu中所有数据
        Iterator<GetGoodsSpuDO> iterator = spuMap.values().iterator();
        while (iterator.hasNext()){
            GetGoodsSpuDO next = iterator.next();
            if (typeName.equals(next.getGoodsType())){
                GoodsHomepage homepage = new GoodsHomepage();
                homepage.setGoods_type_name(next.getGoodsType());
                homepage.setGoods_type_name_spu(next.getGoodsName());
                list2.add(homepage);
            }
        }
        return new RestResult(list2);
    }
}
